﻿using System;
using System.Windows.Forms;
using Elevation.AdminPanel.Presenters;
using Elevation.AdminPanel.Presenters.Impl;
using Elevation.AdminPanel.Properties;
using Elevation.AdminPanel.Views;

namespace Elevation.AdminPanel.Forms
{
	public partial class MainForm : Form, IMainView
	{
		#region Fields

		private readonly IMainPresenter presenter;

		#endregion Fields

		#region Constructors

		public MainForm()
		{
			InitializeComponent();
			presenter = new MainPresenter(this);
		}

		#endregion Constructors

		#region IMainView implementation

		string IMainView.Target
		{
			get { return TargetTextBox.Text; }
			set { TargetTextBox.Text = value; }
		}

		void IMainView.DisableAdminControls()
		{
			AdminUsernameTextBox.Enabled = AdminPasswordTextBox.Enabled = false;
		}

		string IMainView.BrowseForTarget()
		{
			var dialogResult = OpenFileDialog.ShowDialog();
			return dialogResult == DialogResult.OK
			       	? OpenFileDialog.FileName
			       	: null;
		}

		void IMainView.ShowErrorMessage(string message)
		{
			MessageBox.Show(
				this, message, Resources.ErrorMessage_Caption,
				MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

		#endregion IMainView implementation

		#region Event handlers

		private void MainForm_Load(object sender, EventArgs e)
		{
			presenter.OnLoad();
		}

		private void BrowseButton_Click(object sender, EventArgs e)
		{
			presenter.OnBrowse();
		}

		private void StartButton_Click(object sender, EventArgs e)
		{
			presenter.OnStartProcess(AdminUsernameTextBox.Text, AdminPasswordTextBox.Text);
		}

		#endregion Event handlers
	}
}
